/**
 * 
 */
package edu.uwm.cs552.gfx;

import javax.swing.JApplet;
import javax.swing.JOptionPane;

/**
 * @author boyland
 *
 */
public abstract class AnimationApplet extends JApplet {

	private static final long serialVersionUID = 3L;
	
	private int delay = 100;
	
	/** 
	 * Initialize an animation applet.  The delay parameter is used
	 * to set the number of milliseconds between movements (default 100ms).
	 * The applet itself may wish other initializations.  These should
	 * be done using {@link #initialize()}. 
	 * @see java.applet.Applet#init()
	 * @see #initialize()
	 */
	@Override
	public final void init() {
		super.init();
		String ds = getParameter("delay");
		if (ds != null) {
			try {
				delay = Integer.parseInt(ds);
				if (delay <= 0) delay = 1;
			} catch (NumberFormatException ex) {
				JOptionPane.showMessageDialog(this, "Badly formatted 'delay' parameter: " + ds,"Parameter Error", JOptionPane.ERROR_MESSAGE);
			}
		}
		initialize();
		new Thread(new Runnable() {
			public void run() {
				try {
					while (true) {
						Thread.sleep(delay);
						move();
						repaint();
					}
				} catch (InterruptedException e) {
					// just fall through and return
				}
			}
		}).start();
	}

	/**
	 * Initialize the animation applet parameters.
	 * After this method returns, the animation will start.
	 */
	public void initialize() { }

	/**
	 * Called when the animation applet should update 
	 * location information for the applet's animation.
	 */
	public abstract void move();

	
}
